<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form ref="form" label-position="right" label-width="95px" :model="formData" :rules="formRules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="管理员账号" prop="login_name">
            <el-input v-model="formData.login_name" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="管理员姓名" prop="name">
            <el-input v-model="formData.name" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="管理员邮箱" prop="email">
            <el-input v-model="formData.email" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="管理员手机" prop="phone">
            <el-input v-model="formData.phone" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="管理员密码" prop="password">
            <el-input v-model="formData.password" placeholder="请输入" show-password />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="账号状态" prop="status">
            <el-switch v-model="formData.status" active-text="启用" inactive-text="禁用" :active-value="1" :inactive-value="0" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="角色" prop="tags">
        <el-checkbox-group v-model="formData.role_id">
          <el-checkbox v-for="item in list" :key="item.id" :label="item.id" name="type">{{ item.name }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="onSubmit">提交</el-button>
        <el-button size="small" @click="dialogShow = false">关闭</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { addSysUserInfoApi, updateSysUserInfoApi, getroleInfoApi } from '@/api/sys_user'
export default {
  name: 'AddFormRos',
  data() {
    const validateMenu = (rule, values, callback) => {
      this.formData.menuids.length ? callback() : callback(new Error('请勾选'))
    }
    return {
      list: [],
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        email: '',
        login_name: '',
        name: '',
        phone: '',
        password: '',
        status: '',
        role_id: []
      },
      // 表单验证规则
      formRules: {
        email: [{ required: true, message: '请输入', trigger: 'blur' }],
        login_name: [{ required: true, message: '请输入', trigger: 'blur' }],
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入', trigger: 'blur' }],
        password: [{ required: true, message: '请输入', trigger: 'blur' }],
        status: [{ required: true, message: '请输入', trigger: 'blur' }],
        role_id: [{ validator: validateMenu, message: '请勾选', trigger: 'blur' }]
      },
      // 树的数据
      allTreeData: []
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  created() {
    this.getroleInfoApi()
  },
  methods: {
    //  获取角色信息
    async getroleInfoApi() {
      const { data } = await getroleInfoApi()
      console.log(data, '角色')
      this.list = data
    },
    // 提交行为
    async onSubmit() {
      // 表单的校验
      await this.$refs.form.validate()
      // 加工处理表单数据(拷贝一份再加工)
      // 请求的发生
      this.formData.id
        ? await updateSysUserInfoApi(this.formData)
        : await addSysUserInfoApi(this.formData)
      // 提示效果
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      // 关闭弹框
      this.dialogShow = false
      // 列表更新
      this.$emit('updateList')
    },
    // 弹框关闭
    onClose() {
      // 表单重置
      this.formData = {
        email: '',
        login_name: '',
        name: '',
        phone: '',
        password: '',
        status: '',
        role_id: []
      }
      // 移除校验
      this.$refs.form.clearValidate()
    }
  }
}
</script>
<style scoped></style>
